package com.hqbzl.c1;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j
public class TestThreadStatus {
    public static void main1(String[] args) {

        final Thread t1 = new Thread(() -> System.out.println("1"), "t1");
        System.out.println("t1.getState() = " + t1.getState());
        t1.start();
        final Thread t2 = new Thread(() -> {
            synchronized (TestThreadStatus.class) {
                try {
                    TestThreadStatus.class.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("t2 run = " + "t2 run");
        }, "t2");
        t2.start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("t1.getState() = " + t1.getState());
        System.out.println("t2.getState() = " + t2.getState());
        synchronized (TestThreadStatus.class) {
            TestThreadStatus.class.notify();
        }
        System.out.println("t2.getState() = " + t2.getState());
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("t2.getState() = " + t2.getState());
    }

    public static void main(String[] args) {
        Object LOCK = new Object();
        final Thread thread1 = new Thread(() -> {
            synchronized (LOCK) {
                try {
                    log.debug("t1 wait");
                    LOCK.wait();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            log.debug("t1 run");
        }, "t1");
        thread1.start();
        final Thread thread2 = new Thread(() -> {
            synchronized (LOCK) {
                try {
                    log.debug("t2 wait");
                    LOCK.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            log.debug("t2 run");
        }, "t2");
        thread2.start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (LOCK) {
            LOCK.notifyAll();
        }
        System.out.println("thread1.getState() 1= " + thread1.getState());
        System.out.println("thread2.getState() 1= " + thread2.getState());

        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("thread1.getState() 2= " + thread1.getState());
        System.out.println("thread2.getState() 2= " + thread2.getState());
        System.out.println("the end.");
    }
}
